<div class="clr"><br/></div>
<form method="post" name="invoices_form" action="{$GLOBALS.site_url}/manage-invoices/">
	<input type="hidden" name="action_name" id="action_name" value="" />
	<input type="hidden" name="sorting_field" id="sorting_field" value="{$sorting_field}" />
	<input type="hidden" name="sorting_order" id="sorting_order" value="{$sorting_order}" />
    <input type="hidden" name="page" id="page" value="{$currentPage}" />
	<div class="box" id="displayResults">
		<div class="box-header">
			<div style="display:inline-block;">
				<div class="actionSelected">
					[[Actions with Selected:]]
					<select id="selectedAction_up" name="selectedAction_up">
						<option value="">[[Select action]]</option>
						<option value="paid">[[Mark Paid]]</option>
						<option value="unpaid">[[Mark Unpaid]]</option>
						<option value="delete">[[Delete]]</option>
					</select>
					<input type="button" value="Go" class="grayButton" onclick="go('up');"/>
				</div>
			</div>
			<div class="pagination">
			{foreach from=$pages item=page}
				{if $page == $currentPage}
					<strong>{$page}</strong>
				{else}
					{if $page == $totalPages && $currentPage < $totalPages-3} ... {/if}
					<a href="?page={$page}{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}&amp;invoices_per_page={$invoices_per_page}{$searchFields}">{$page}</a>
					{if $page == 1 && $currentPage > 4} ... {/if}
				{/if}
			{/foreach}
			</div>
			<div class="numberPerPage">
				[[per page:]]
				<select id="invoices_per_page" name="invoices_per_page" onchange="window.location = '?restore=1{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}&invoices_per_page='+this.value;" class="perPage">
					<option value="10" {if $invoices_per_page == 10}selected{/if}>10</option>
					<option value="20" {if $invoices_per_page == 20}selected{/if}>20</option>
					<option value="50" {if $invoices_per_page == 50}selected{/if}>50</option>
					<option value="100" {if $invoices_per_page == 100}selected{/if}>100</option>
				</select>
				<input type="hidden" name="restore" value="1" />
				<input type="hidden" name="page" value="1" />
			</div>
		</div>
		<div class="innerpadding">
			<div id="displayResultsTable">
				<table width="100%">
					<thead>
					<tr>
						<th><input type="checkbox" id="all_checkboxes_control"></th>
						<th>
							<a href="?restore=1&amp;sorting_field=sid&amp;sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'sid'}DESC{else}ASC{/if}&amp;invoices_per_page={$invoices_per_page}&amp;page={$currentPage}">[[Invoice]]&nbsp;#</a>
							{if $sorting_field == 'sid'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif" />{else}<img src="{image}b_up_arrow.gif" />{/if}{/if}
						</th>
						<th>
							<a href="?restore=1&amp;sorting_field=username&amp;sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'username'}DESC{else}ASC{/if}&amp;invoices_per_page={$invoices_per_page}&amp;page={$currentPage}">[[Customer Name]]</a>
							{if $sorting_field == 'username'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif" />{else}<img src="{image}b_up_arrow.gif" />{/if}{/if}
						</th>
						<th>
							<a href="?restore=1&amp;sorting_field=date&amp;sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'date'}DESC{else}ASC{/if}&amp;invoices_per_page={$invoices_per_page}&amp;page={$currentPage}">[[Date]]</a>
							{if $sorting_field == 'date'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif" />{else}<img src="{image}b_up_arrow.gif" />{/if}{/if}
						</th>
						<th>
							<a href="?restore=1&amp;sorting_field=payment_method&amp;sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'payment_method'}DESC{else}ASC{/if}&amp;invoices_per_page={$invoices_per_page}&amp;page={$currentPage}">[[Payment Method]]</a>
							{if $sorting_field == 'payment_method'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif" />{else}<img src="{image}b_up_arrow.gif" />{/if}{/if}
						</th>
						<th>
							<a href="?restore=1&amp;sorting_field=total&amp;sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'total'}DESC{else}ASC{/if}&amp;invoices_per_page={$invoices_per_page}&amp;page={$currentPage}">[[Total]]</a>
							{if $sorting_field == 'total'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif" />{else}<img src="{image}b_up_arrow.gif" />{/if}{/if}
						</th>
						<th>
							<a href="?restore=1&amp;sorting_field=status&amp;sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'status'}DESC{else}ASC{/if}&amp;invoices_per_page={$invoices_per_page}&amp;page={$currentPage}">[[Status]]</a>
							{if $sorting_field == 'status'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif" />{else}<img src="{image}b_up_arrow.gif" />{/if}{/if}
						</th>
						<th colspan="2" width="1%">
							[[Actions]]
						</th>
					</tr>
					</thead>
					<tbody>
					{foreach from=$found_invoices item=invoice name=invoices_block}
					<tr class="{cycle values="oddrow,evenrow"}">
						<td><input type="checkbox" name="invoices[{$invoice.sid}]" value="1" id="checkbox_{$smarty.foreach.invoices_block.iteration}" /></td>
						<td><a href="{$GLOBALS.site_url}/edit-invoice/?sid={$invoice.sid}">{display property='sid' object_sid=$invoice.sid}</a></td>
						<td>
							{display property='subuser_sid' object_sid=$invoice.sid assign=subuser}
							{if $invoice.userExists}
								{if $subuser}
									<a href="{$GLOBALS.site_url}/edit-user/?user_sid={$subuser}">{display property='username' object_sid=$invoice.sid}</a>
								{else}
									<a href="{$GLOBALS.site_url}/edit-user/?user_sid={display property='user_sid' object_sid=$invoice.sid}">{display property='username' object_sid=$invoice.sid}</a>
								{/if}
							{else}
								<span class="invoice-washy">[[User deleted]]</span>
							{/if}
						</td>
						<td>{display property='date' object_sid=$invoice.sid}</td>
						<td>{display property='payment_method' object_sid=$invoice.sid}</td>
						<td>{$GLOBALS.settings.transaction_currency}{display property='total' object_sid=$invoice.sid}</td>
						<td>{display property='status' object_sid=$invoice.sid}</td>
						<td>
						{if $invoice.status != 'Paid'}
							<input type="button" name="action" value="[[Mark Paid]]" class="editbutton"  onclick="$('#checkbox_{$smarty.foreach.invoices_block.iteration}').attr('checked', 'checked'); submitForm('paid');" style="text-align: center;"/>
						{else}
							<input type="button" name="action" value="[[Mark Unpaid]]" class="editbutton" onclick="$('#checkbox_{$smarty.foreach.invoices_block.iteration}').attr('checked', 'checked'); submitForm('unpaid');" style="text-align: center;"/>
						{/if}
						</td>
						<td><input type="button" name="action" value="Delete" class="deletebutton" onclick="if (confirm('Are you sure you want to delete this invoice?')) {ldelim}$('#checkbox_{$smarty.foreach.invoices_block.iteration}').attr('checked', 'checked'); submitForm('delete');{rdelim}" /></td>

					</tr>
					{/foreach}
					</tbody>
				</table>
			</div>
		</div>
		<div class="box-footer">
			<div style="display:inline-block;">
				<div class="actionSelected">
					[[Actions with Selected:]]
					<select id="selectedAction_up" name="selectedAction_up">
						<option value="">[[Select action]]</option>
						<option value="paid">[[Mark Paid]]</option>
						<option value="unpaid">[[Mark Unpaid]]</option>
						<option value="delete">[[Delete]]</option>
					</select>
					<input type="button" value="Go" class="grayButton" onclick="go('down');"/>
				</div>
			</div>
			<div class="pagination">
			{foreach from=$pages item=page}
				{if $page == $currentPage}
					<strong>{$page}</strong>
					{else}
					{if $page == $totalPages && $currentPage < $totalPages-3} ... {/if}
					<a href="?page={$page}{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}&amp;invoices_per_page={$invoices_per_page}{$searchFields}">{$page}</a>
					{if $page == 1 && $currentPage > 4} ... {/if}
				{/if}
			{/foreach}
			</div>
			<div class="numberPerPage">
				[[per page:]]
				<select id="invoices_per_page" name="invoices_per_page" onchange="window.location = '?restore=1{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}&invoices_per_page='+this.value;" class="perPage">
					<option value="10" {if $invoices_per_page == 10}selected{/if}>10</option>
					<option value="20" {if $invoices_per_page == 20}selected{/if}>20</option>
					<option value="50" {if $invoices_per_page == 50}selected{/if}>50</option>
					<option value="100" {if $invoices_per_page == 100}selected{/if}>100</option>
				</select>
				<input type="hidden" name="restore" value="1" />
				<input type="hidden" name="page" value="1" />
			</div>
		</div>
	</div>
</form>
<div id="dialog" style="display: none"></div>
<script>
	var total={$smarty.foreach.invoices_block.total};
	{literal}

	function set_checkbox(param) {
		for (i = 1; i <= total; i++) {
			if (checkbox = document.getElementById('checkbox_' + i))
				checkbox.checked = param;
		}
	}

	$("#all_checkboxes_control").click(function() {
		if ( this.checked == false)
			set_checkbox(false);
		else
			set_checkbox(true);
	});

	function submitForm(action) {
		document.getElementById('action_name').value = action;
		var form = document.invoices_form;
		form.submit();
	}
	function go( button ){
		if($("input:checked").length > 0 && $("#selectedAction_"+button).val() != ''){
			var action = $("#selectedAction_"+button).val();

			switch ( action ) {
				case 'paid':
					submitForm(action);
					break;
				case 'unpaid':
					submitForm(action);
					break;
				case 'delete':
					if ( confirm('Are you sure you want to delete selected invoice(s)?') ){
						submitForm(action);
						break;
					}
			}
		} else {
			$(function() {
				$("#dialog").dialog('destroy');
				$("#dialog").attr({title: "Information"});
				$("#dialog").html("Please choose an action first").dialog({width: 300});
			});
		}
	}
	{/literal}
</script>
